package br.uvv.Web.ManagedBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.primefaces.context.RequestContext;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import br.uvv.Model.Ator;
import br.uvv.Service.AtorService;
import br.uvv.Util.InitUtil;
import br.uvv.Util.RNException;
import br.uvv.Util.Util;

public class AtorBean implements Serializable {

	private static final long serialVersionUID = -4987790603498712639L;

	private AtorService service;
	
	private Ator atorInstance;
	private Ator atorInstanceTemp;
	private LazyDataModel<Ator> atorList;
	private Ator[] atorSelect;
	
	private String varBuscaAtor;
	private String msgExclusao;
	
	public String ator(){
		init();
		return "/views/atores/show.xhtml";
	}
	
	private void init(){
		service = new AtorService();
		atorInstance = new Ator();
		atorInstanceTemp = new Ator();
		varBuscaAtor = "";
		atorList = InitUtil.inicializaAtorList();
		atorSelect = new Ator[0];
		filtrar();
	}
	
	public void goCreate(){
		atorInstance = new Ator();
	}
	
	public void salvar(){
		try {
			service.salvarAtor(atorInstance);
//			MsgUtil.msgSucesso("sal", atorInstance.getNome());
		} catch (RNException e){
//			MsgUtil.msgErro("atu", atorInstance.getNome());
		}
	}
	
	public void goEdit(){
		
	}
	
	public void atualizar(){
		try {
			service.atualizarAtor(atorInstance);
//			MsgUtil.msgSucesso("atu", atorInstance.getNome());
		} catch (RNException e){
//			MsgUtil.msgErro("atu", atorInstance.getNome());
		}
	}
	
	public void goDelete(){
		RequestContext contexto = RequestContext.getCurrentInstance();
		Integer size = atorSelect.length;
		if (size == 0){
			contexto.execute("dialogoSelecione.show();");
			return;
		}
		if (size == 1){
			for (Ator ator: atorSelect){
				msgExclusao = "Excluir unico " + ator.getNome();
			}
		} else {
			String msg = "";
			Integer count = 0;
			for (Ator ator:atorSelect){
				msg += "'" + ator.getNome() + "'";
				count++;
				if (count==(size-1))
					msg += " e ";
				else if (count != size)
					msg += ", ";
			}
			msgExclusao = msg + " - Excluir multiplo";
		}
		contexto.execute("dialogoExcluir.show();");
	}
	
	public void excluir(){
		for (Ator ator:atorSelect) {
			try {
				service.removerAtor(ator);
//				MsgUtil.msgSucesso("del", ator.getNome());
			} catch (RNException e){
//				MsgUtil.msgErro("del", ator.getNome());
			}
		}
	}
	
	@SuppressWarnings("serial")
	public void filtrar(){
		atorList = new LazyDataModel<Ator>() {
			@Override
			public List<Ator> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				Util.setDataTableRows(pageSize);
				try {
					return service.listarAtor(first, pageSize, sortField, sortOrder, varBuscaAtor);
				} catch (RNException e) {
					e.printStackTrace();
					return new ArrayList<Ator>();
				}
			}
			@Override  
	        public Ator getRowData(String rowKey)  
	        {  
	        	try {
		            return service.selecionarAtor(Long.parseLong(rowKey));
	        	} catch (RNException e) {
	        		e.printStackTrace();
	        		return new Ator();
	        	} catch (NumberFormatException e){
	        		return new Ator();
	        	}
	        }  
	        @Override  
	        public String getRowKey(Ator objeto)  
	        {  
	        	if (objeto!=null)
	        		return ""+objeto.getId();
	        	return "";
	        }  
		};
		try {
			atorList.setRowCount(service.countAtor(varBuscaAtor));
		} catch (RNException e) {
			e.printStackTrace();
		}
		atorList.setPageSize(5);
	}
	
	@SuppressWarnings("serial")
	public void filtrarTodos(){
		atorList = new LazyDataModel<Ator>() {
			@Override
			public List<Ator> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
				Util.setDataTableRows(pageSize);
				try {
					return service.listarAtor(first, pageSize, sortField, sortOrder, null);
				} catch (RNException e) {
					e.printStackTrace();
					return new ArrayList<Ator>();
				}
			}
			@Override  
	        public Ator getRowData(String rowKey)  
	        {  
	        	try {
		            return service.selecionarAtor(Long.parseLong(rowKey));
	        	} catch (RNException e) {
	        		e.printStackTrace();
	        		return new Ator();
	        	} catch (NumberFormatException e){
	        		return new Ator();
	        	}
	        }  
	        @Override  
	        public String getRowKey(Ator objeto)  
	        {  
	        	if (objeto!=null)
	        		return ""+objeto.getId();
	        	return "";
	        }  
		};
		try {
			atorList.setRowCount(service.countAtor(null));
		} catch (RNException e) {
			e.printStackTrace();
		}
		atorList.setPageSize(5);
	}
	
	public String goHome(){
		service = null;
		atorInstance = null;
		atorInstanceTemp = null;
		varBuscaAtor = null;
		atorList = null;
		return "goHome";
	}

	public Ator getAtorInstance() {
		return atorInstance;
	}

	public void setAtorInstance(Ator atorInstance) {
		this.atorInstance = atorInstance;
	}

	public Ator getAtorInstanceTemp() {
		return atorInstanceTemp;
	}

	public void setAtorInstanceTemp(Ator atorInstanceTemp) {
		this.atorInstanceTemp = atorInstanceTemp;
	}

	public LazyDataModel<Ator> getAtorList() {
		return atorList;
	}

	public void setAtorList(LazyDataModel<Ator> atorList) {
		this.atorList = atorList;
	}

	public String getVarBuscaAtor() {
		return varBuscaAtor;
	}

	public void setVarBuscaAtor(String varBuscaAtor) {
		this.varBuscaAtor = varBuscaAtor;
	}
	
	public Ator[] getAtorSelect() {
		return atorSelect;
	}
	
	public void setAtorSelect(Ator[] atorSelect) {
		this.atorSelect = atorSelect;
	}
	
	public String getMsgExclusao() {
		return msgExclusao;
	}
	
	public void setMsgExclusao(String msgExclusao) {
		this.msgExclusao = msgExclusao;
	}
}